package com.base.kotlin

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.transform
import org.junit.Test

import org.junit.Assert.*
import kotlinx.coroutines.GlobalScope
/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */

class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        //assertEquals(4, 2 + 2)


          a()
        //main()
    }
   fun a()= runBlocking {



//       launch {
//           delay(1000)
//           println("1")
//       }
//       launch {
//          launch {
//              delay(1000)
//              println("3")
//          }
//           println("2")
//       }
//       val request = launch {
//           // it spawns two other jobs
//
//           launch() {
//               println("job1: I run in my own Job and execute independently!")
//               delay(1000)
//               println("job1: I am not affected by cancellation of the request")
//           }
//           // and the other inherits the parent context
//           launch {
//               delay(100)
//               println("job2: I am a child of the request coroutine")
//               delay(1000)
//               println("job2: I will not execute this line if my parent request is cancelled")
//           }
//           withContext(Dispatchers.Default){
//               println("job3: I run in my own Job and execute independently!")
//               delay(1000)
//               println("job3: I am not affected by cancellation of the request")
//           }
//
//       }
//       delay(500)
//       request.cancel() // cancel processing of the request
//       delay(1000) // delay a second to see what happens
       val job=launch {
           Job()
       }



       println("hello")
       job.join()
       println("nmsl")
      // doWorld()
//       val job= launch {
//            println("word")
//        }
//       val job1= launch {
//           println("word2")
//       }
//       println("hello")
//       println("done")

   }


    suspend fun Job(){
        delay(5000)
        println("world")

    }
    suspend fun doWorld() = coroutineScope { // this: CoroutineScope

        doWorld1()
        async {

            println("Worlds 2")
        }.await()
        async {
            println("Worlds 1")
        }.await()
        println("Hello1")
    }

    suspend fun doWorld1() = coroutineScope { // this: CoroutineScope
        async {

            println("Worlds 3")
        }
        async {

            println("Worlds 4")
        }
        println("Hello2")
    }

//
//    suspend fun simple(): List<Int> {
//        delay(1000) // 假装我们在这里做了一些异步的事情
//        return listOf(1, 2, 3)
//    }
fun simple(): Sequence<Int> = sequence { // 序列构建器
    for (i in 1..3) {
        Thread.sleep(100) // 假装我们正在计算
        yield(i) // 产生下一个值
    }
}



}